package com.controller;

import com.entity.Blog;
import com.entity.Blogger;
import com.entity.PageBean;
import com.service.BlogService;
import com.service.BloggerService;
import com.util.PageUtil;
import com.util.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主页Contrller
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/")
public class IndexController {

    @Resource
    private BlogService blogService;
    @Resource
    public BloggerService bloggerService;
    /**
     * 请求主页
     * @return
     */
    @RequestMapping("/index/{id}.htm")
    public ModelAndView index(@PathVariable("id") Integer id, @RequestParam(value="page",required=false)String page, HttpServletRequest request, @RequestParam(value="typeId",required=false)String typeId, @RequestParam(value="releaseDateStr",required=false)String releaseDateStr)throws Exception{
        ModelAndView mav=new ModelAndView();
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        PageBean pageBean=new PageBean(Integer.parseInt(page),10);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("typeId",typeId);
        map.put("releaseDateStr",releaseDateStr);
        map.put("bloggerId", id);
        List<Blog> blogList=blogService.list(map);
        System.out.println(blogList);
        //博客首页缩略图
        for(Blog blog:blogList){
            //创建图片集合
            List<String> imageList=blog.getImageList();
            //博客信息content
            String blogInfo=blog.getContent();
            //Jsoup截图图片jpg提取
            Document doc= Jsoup.parse(blogInfo);
            Elements jpgs=doc.select("img[src$=.jpg]");
            Elements pngs=doc.select("img[src$=.png]");
            for(int i=0;i<jpgs.size();i++){
                Element jpg=jpgs.get(i);
                imageList.add(jpg.toString());
                if(i==2){
                    break;
                }
            }
            for(int i=0;i<pngs.size();i++){
                Element png=pngs.get(i);
                imageList.add(png.toString());
                if(i==2){
                    break;
                }
            }
        }
        StringBuffer str = new StringBuffer();
        //传入typeId参数查询
        if(StringUtil.isNotEmpty(typeId)){
            str.append("typeId="+typeId+"&");
        }
        //传入releaseDateStr参数查询
        if(StringUtil.isNotEmpty(releaseDateStr)){
            str.append("releaseDateStr="+releaseDateStr+"&");
        }
        Blogger blogger=bloggerService.getByUserId(id);
        System.out.println(blogger);
        mav.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/index/"+id+".htm",blogService.getTotal(map),Integer.parseInt(page),10,str.toString()));
        mav.addObject("blogList", blogList);
        mav.addObject("pageTitle", "——CV博客系统");
        mav.addObject("bloggerId",id);
        mav.addObject("blogger",blogger);
        mav.addObject("mainPage", "/foreground/blog/list.jsp");
        mav.setViewName("/pages/blogg/mainTemp");
        return mav;
    }
        /**
         * 源码下载
         * @author Administrator
         *
         */
        @RequestMapping("/baseCode")
        public ModelAndView baseCode() throws Exception{
            ModelAndView mav=new ModelAndView();
            mav.addObject("pageTitle", "源码下载—倾城博客系统");
            mav.addObject("mainPage", "foreground/base/baseCode.jsp");
            mav.setViewName("pages/blogg/mainTemp");
            return mav;
        }
}
